<template>
    <div>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        <el-breadcrumb-item>权限列表</el-breadcrumb-item>
      </el-breadcrumb>

      <el-card>
        <el-button type="primary" class="addRolesBun" @click="addRoles">添加角色</el-button>

        <!--        表格区域-->
        <el-table :data="list" stripe border>
          <el-table-column width="45px" type="expand">
            <template slot-scope="scope" >
              <el-row   v-for="(item,index) in scope.row.children" :key="item.id" :class="['ver','bgtop','bgborder']">
                <el-col :span="5" :class="['bgborder',index === '0' ? 'bgtop' : '']" >
                    <el-tag>{{item.authName}}</el-tag>
                    <i class="el-icon-arrow-right"></i>
                </el-col>
                <el-col :span="19">
                    <el-row v-for="(item2,index2) in item.children" :key="item2.id" :class="['ver']">
                      <el-col :span="6" :class="['bgborder',index2 === '0' ? '' : 'bgtop']">
                        <el-tag type="success">{{item2.authName}}</el-tag>
                        <i class="el-icon-arrow-right"></i>
                      </el-col>
                      <el-col :span="18">
                        <el-row v-for="(item3) in item2.children" :key="item3.id">
                          <el-col :class="['bgborder']">
                            <el-tag type="warning" closable @close="deletelimit(scope.row,item3.id)">{{item3.authName}}</el-tag>
                            <i class="el-icon-arrow-right"></i>
                          </el-col>
                        </el-row>
                      </el-col>
                    </el-row>
                </el-col>
              </el-row>

            </template>
          </el-table-column>
          <el-table-column type="index" label="#" width="45px"></el-table-column>
          <el-table-column label="角色名称" prop="roleName"></el-table-column>
          <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" size="small"  @click="editRoles(scope.row.id)">编辑</el-button>
              <el-button type="danger" icon="el-icon-delete" size="small" @click="deleteRoles(scope.row.id)">删除</el-button>
              <el-button type="warning" icon="el-icon-share" size="small" @click="showRoles(scope.row)">分配权限</el-button>
            </template>
          </el-table-column>
        </el-table>

      </el-card>


<!--      添加角色的信息框-->
      <el-dialog
        title="添加角色"
        :visible.sync="adddialogVisible"
        width="50%">

<!--        表单区域-->
        <el-form ref="form" :model="addrolesdate" label-width="70px">
          <el-form-item label="角色名称">
            <el-input v-model="addrolesdate.roleName"></el-input>
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="addrolesdate.roleDesc"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
    <el-button @click="adddialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addrolesbtn">确 定</el-button>
  </span>
      </el-dialog>


<!--编辑角色的信息框      -->
      <el-dialog
        title="编辑角色信息"
        :visible.sync="editdialogVisible"
        width="50%">

        <!--        表单区域-->
        <el-form ref="form" :model="editrolesdate" label-width="70px">
          <el-form-item label="角色名称">
            <el-input v-model="editrolesdate.roleName"></el-input>
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="editrolesdate.roleDesc"></el-input>
          </el-form-item>
        </el-form>


        <span slot="footer" class="dialog-footer">
    <el-button @click="editdialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editRolesData">确 定</el-button>
  </span>
      </el-dialog>

<!--      分配权限的弹出层-->
        <el-dialog
          title="分配权限"
          :visible.sync="showdialogVisible"
          width="50%" @close="closearry">

          <el-tree :data="showdata" :props="treeProps" default-expand-all show-checkbox node-key="id" :default-checked-keys="keyarry" ref="fpRef"></el-tree>


          <span slot="footer" class="dialog-footer">
        <el-button @click="showdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="fpqx">确 定</el-button>
          </span>
      </el-dialog>
    </div>
</template>
<script>
export default {
  data() {
    return {
      list: [],
      addrolesdate: {
        roleName: '',
        roleDesc: ''
      },
      adddialogVisible: false,
      editdialogVisible: false,
      showdialogVisible: false,
      editrolesdate: {
        id: '',
        roleName: '',
        roleDesc: ''
      },
      showdata: [],
      treeProps: {
        children: 'children',
        label: 'authName'
      },
      keyarry: [],
      fpid: ''
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const {data: res} = await this.$http.get(`roles`)
      if (res.meta.status !== 200) return this.$message.error('获取角色列表失败')
      this.list = res.data
    },
    addRoles() {
      this.adddialogVisible = !this.adddialogVisible
    },
    addrolesbtn: async function () {
      const {data: res} = await this.$http.post('roles', this.addrolesdate)
      if (res.meta.status !== 201) return this.$message.error('添加失败')
      this.getRoleList()
      this.$message.success('添加成功')
      this.adddialogVisible = false
    },
    async deleteRoles(id) {
      const results = await this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
      if (results !== 'confirm') return false
      const {data: res} = await this.$http.delete(`roles/${id}`)
      if (res.meta.status !== 200) return this.$message.error('删除失败')
      this.getRoleList()
      this.$message.success('删除成功')
    },
    async editRoles(id) {
      this.editdialogVisible = !this.editdialogVisible
      const {data: res}  = await this.$http.get(`roles/${id}`)
      if (res.meta.status !== 200) return this.$message.error('获取角色信息失败')
      this.editrolesdate.roleName = res.data.roleName
      this.editrolesdate.roleDesc = res.data.roleDesc
      this.editrolesdate.id = res.data.roleId
    },
    async editRolesData() {
      const {data: res}  = await this.$http.put(`roles/${this.editrolesdate.id}`,this.editrolesdate)
      if (res.meta.status !== 200) return this.$message.error('修改角色信息失败')
      this.$message.success('修改角色信息成功')
      this.getRoleList()
      this.editdialogVisible = !this.editdialogVisible
    },
    async deletelimit(roleid,rightid) {
      const results = await this.$confirm('此操作将删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
      if (results !== 'confirm') return false
      const {data: res} = await this.$http.delete(`roles/${roleid.id}/rights/${rightid}`)
      if (res.meta.status !== 200) return this.$message.error('删除角色权限失败')
      this.$message.success('删除角色权限成功')
      this.getRoleList()
    },
    async showRoles(role) {
      this.fpid = role.id
      this.showdialogVisible = !this.showdialogVisible
      const {data: res} = await this.$http.get("rights/tree")
      if (res.meta.status !== 200) return this.$message.error('获取角色权限失败')
      this.getroleid(role,this.keyarry)
      this.showdata = res.data
    },
    getroleid(node,arry) {
      if ( !node.children) {
        return arry.push(node.id)
      }
      node.children.forEach(item => {
        this.getroleid(item,arry)
      })
    },
    closearry() {
      this.keyarry = []
    },
    async fpqx() {
      let ary = [
        ...this.$refs.fpRef.getCheckedKeys(),
        ...this.$refs.fpRef.getHalfCheckedKeys()
      ]
      const rids = ary.join(',')
      const {data: res} = await this.$http.post(`roles/${this.fpid}/rights`,{ rids: rids})
      if (res.meta.status !== 200) return this.$message.error('分配权限失败')
      this.$message.success('分配权限成功')
      this.getRoleList()
      this.showdialogVisible = !this.showdialogVisible
    }
  }
}
</script>

<style scoped>
.el-breadcrumb {
  margin: 0 0 15px 15px;
}
.el-pagination {
  margin-top: 10px;
}
.addRolesBun {
  margin-bottom: 20px;
}
.el-tag {
  margin: 7px;
}

.bgtop {
  border-top: 1px solid #eee;
}
.bgborder {
  border-bottom: 1px solid #eee;
}
.ver {
  display: flex;
  align-items: center;
}
</style>
